package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.ps.data.DomainItem;
import com.szholly.utils.util.TreeDataNode;
import com.szholly.data.common.util.TreeDataUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

@Service
public class SwDomainService {
	/**
	 * 获取字典（无分页）
	 * @param domainName 字典名称
	 * @return 字典项列表
	 */
	public List<RowBase> getDomain(String domainName){
		String sql = " select * from DATA_DEFULATDOMAIN t where t.S_DOMAINNAME=@DOMAINNAME order by t.I_ORDER,t.s_domaincode";
		List<GlobalParameter> params = new ArrayList<>();
		params.add(new GlobalParameter("DOMAINNAME",domainName));
		return DataUtil.getSelectRows(sql, params);
	}
	/**
	 * 获取字典（无分页）
	 * @param domainName 字典名称
	 * @param level 某一级别
	 * @return 字典项列表
	 */
	public List<RowBase> getDomain(String domainName,String level){
		String sql = " select * from DATA_DEFULATDOMAIN t where t.S_DOMAINNAME=@DOMAINNAME  and t.I_JB=@level order by t.I_ORDER,t.s_domaincode";
		List<GlobalParameter> params = new ArrayList<>();
		params.add(new GlobalParameter("DOMAINNAME",domainName));
		params.add(new GlobalParameter("level", level));
		return DataUtil.getSelectRows(sql, params);
	}
	/**
	 * 获取字典（无分页）
	 * @param domainName 字典名称
	 * @return JSON字符串，方便返回给浏览器
	 */
	public String getDomainJSON(String domainName){
		ITable pTable = null;
		return DataUtil.listToJSONObject(getDomain(domainName), pTable, null).toString();
	}
	/**
	 * 获取字典（无分页）
	 * @param domainName 字典名称
	 * @param level 某一级别
	 * @return JSON字符串，方便返回给浏览器
	 */
	public String getDomainJSON(String domainName,String level){
		ITable pTable = null;
		return DataUtil.listToJSONObject(getDomain(domainName,level), pTable,null).toString();
	}
	
	/**
	 * 获取三级以内的行政区划：深圳市，各区
	 * @return
	 */
	public List<RowBase> getXZQHDomain() {
		String sql = DataUtil.CACHE + "select  t.s_id,t.s_pid,t.s_domaincode,t.s_domaincaption  from data_defulatdomain t where t.s_domainname=@DOMAINNAME and t.I_JB<3 order by t.I_ORDER,t.s_domaincode";
		List<GlobalParameter> params = new ArrayList<>();
		params.add(new GlobalParameter("DOMAINNAME","政区"));
		return DataUtil.getSelectRows(sql, params);
	}
	
	/**
	 * 获取运维项字典 的 所有项
	 */
	private volatile List<DomainItem> operationItems = null;
	public List<DomainItem> getOperationItems(){
		if(operationItems == null){
			synchronized(this){
				if(operationItems == null){
					String sql = DataUtil.CACHE + "select t.s_domaincode as code,t.s_domaincaption as name,t.s_id as id,t.s_pid as pid from data_defulatdomain t where t.s_domainname = '地表水运维项'";
					List<RowBase> list = DataUtil.getSelectRows(sql, null);
					List<TreeDataNode> tree = TreeDataUtils.GetTreeData(list, "ID", "PID", "CODE", "NAME");
					operationItems = convert(tree);
				}
			}
		}
		return operationItems;
	}
	/**
	 * 获取运维项字典 的 所有项---供水
	 */
	private volatile List<DomainItem> WateroperationItems = null;
	public List<DomainItem> getWaterOperationItems(){
		if(WateroperationItems == null){
			synchronized(this){
				if(WateroperationItems == null){
					String sql = DataUtil.CACHE + "select t.s_domaincode as code,t.s_domaincaption as name,t.s_id as id,t.s_pid as pid from data_defulatdomain t where t.s_domainname = '供水运维项'";
					List<RowBase> list = DataUtil.getSelectRows(sql, null);
					List<TreeDataNode> tree = TreeDataUtils.GetTreeData(list, "ID", "PID", "CODE", "NAME");
					WateroperationItems = convert(tree);
				}
			}
		}
		return WateroperationItems;
	}
	
	/**
	 * 获取项字典 的 所有项
	 */
	public List<DomainItem> getDomainItems(){
		if(operationItems == null){
			synchronized(this){
				if(operationItems == null){
					String sql = DataUtil.CACHE + "select t.s_domaincode as code,t.s_domaincaption as name,t.s_id as id,t.s_pid as pid from data_defulatdomain t where t.s_domainname = 'OPERATION_MATERIAL_UNIT'";
					List<RowBase> list = DataUtil.getSelectRows(sql, null);
					List<TreeDataNode> tree = TreeDataUtils.GetTreeData(list, "ID", "PID", "CODE", "NAME");
					operationItems = convert(tree);
				}
			}
		}
		return operationItems;
	}
	
	/**
	 * 根据名称，获取目标节点
	 * @param nodes
	 * @param name
	 * @return
	 */
	public DomainItem getTargetItem(List<DomainItem> nodes,String name){
		if(nodes == null || StringUtil.IsNullOrEmpty(name)) return null;
		for(DomainItem node : nodes){
			if(name.equals(node.getName())){
				return node;
			}
			else{
				DomainItem item = getTargetItem(node.getChildren(),name);
				if(item!=null) return item;
			}
		}
		return null;
	}
	
	/**
	 * 把原来的字典树的复杂节点，转换成只包含CODE和NAME属性的节点
	 * @param tree
	 * @return
	 */
	private List<DomainItem> convert(List<TreeDataNode> tree){
		List<DomainItem> items = new ArrayList<DomainItem>();
		DomainItem item;
		RowBase row;
		for(TreeDataNode node : tree){
			row = (RowBase) node.getAttributes();
			item = new DomainItem(row.getSafeString("CODE"),row.getSafeString("NAME"));
			convertChildren(node,item);
			items.add(item);
		}
		
		return items;
	}
	
	/**
	 * 递归转换子节点
	 * @param node
	 * @param item
	 */
	private void convertChildren(TreeDataNode node,DomainItem item){
		List<TreeDataNode> list = node.getChildren();
		if(list == null || list.size()==0) return;
		
		for(TreeDataNode cNode:node.getChildren()){
			RowBase row = (RowBase) cNode.getAttributes();
			DomainItem cItem = new DomainItem(row.getSafeString("CODE"),row.getSafeString("NAME"));
			item.getChildren().add(cItem);
			convertChildren(cNode,cItem);
		}
	}
	
	/**
	 * 获取日常巡检的运维项：首先获取所有的运维项，然后过滤出日常巡检的运维项--污水厂
	 * @return
	 */
	public String getDailyInspectionItems(){
		List<DomainItem> list = getOperationItems();
		DomainItem result = getTargetItem(list,"日常巡检");
		if(result == null) return "";
		String json = JsonUtil.ConvertArrayToJson(result.getChildren());
		return json;
	}
	/**
	 * 获取日常巡检的运维项：首先获取所有的运维项，然后过滤出日常巡检的运维项--供水
	 * @return
	 */
	public String getWaterDailyInspectionItems(){
		List<DomainItem> list = getWaterOperationItems();
		DomainItem result = getTargetItem(list,"日常巡检");
		if(result == null) return "";
		String json = JsonUtil.ConvertArrayToJson(result.getChildren());
		return json;
	}
	/**
	 * 获取定期维护的运维项：首先获取所有的运维项，然后过滤出定期维护的运维项---污水厂
	 * @return
	 */
	public String getPeriodMaintainanceItems(){
		List<DomainItem> list = getOperationItems();
		DomainItem result = getTargetItem(list,"定期维护");
		if(result == null) return "";
		String json = JsonUtil.ConvertArrayToJson(result.getChildren());
		return json;
	}
	/**
	 * 获取定期维护的运维项：首先获取所有的运维项，然后过滤出定期维护的运维项---供水
	 * @return
	 */
	public String getSupplyWaterMaintainanceItems(){
		List<DomainItem> list = getWaterOperationItems();
		DomainItem result = getTargetItem(list,"定期维护");
		if(result == null) return "";
		String json = JsonUtil.ConvertArrayToJson(result.getChildren());
		return json;
	}
	/**
	 * 根据字典id获取字典项
	 * @return
	 */
	public String getDomainById(String code){
		List<DomainItem> list = getDomainItems();
		DomainItem result = getTargetItem(list,code);
		if(result == null) return "";
		String json = JsonUtil.ConvertArrayToJson(result.getChildren());
		return json;
	}
}
